package vn.lumi.smarthome.ui.device;

import net.margaritov.preference.colorpicker.ColorPickerDialog;
import net.margaritov.preference.colorpicker.ColorPickerDialog.OnColorChangedListener;
import net.margaritov.preference.colorpicker.ColorPickerDialog.OnTouchReleasedListener;
import net.margaritov.preference.colorpicker.ColorPickerPanelView;
import vn.lumi.smarthome.Config;
import vn.lumi.smarthome.R;
import vn.lumi.smarthome.io.model.Device;
import vn.lumi.smarthome.service.VibrateThread;
import vn.lumi.smarthome.ulti.AnimationUlti;
import vn.lumi.smarthome.ulti.LogUltis;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class DeviceListAdapter extends BaseAdapter {
	private static final String TAG = "DeviceListAdapter";
	private ColorPickerDialog mDialog;
	private Context mContext;
	private Device[] mDevices;
	boolean ischeckTypeOnOff;
	boolean ischeckTypeRGB;
	boolean ischeckTypeBlind;
	boolean ischeckTypeDimmer;

	public DeviceListAdapter(Context context, Device[] devices) {
		mContext = context;
		mDevices = new Device[devices.length];
		for (int i = 0; i < devices.length; i++) {
			mDevices[i] = new Device(devices[i]);
		}
	}

	@Override
	public int getCount() {
		return mDevices.length;
	}

	@Override
	public Object getItem(int arg0) {
		return mDevices[arg0];
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	//
	@Override
	public int getViewTypeCount() {
		return Device.NUM_LAYOUT_TYPE;
	}

	//
	@Override
	public int getItemViewType(int position) {
		// LogUltis.i(TAG, "View Type="+(mDevices[position].getType_id()));
		return Device.getLayoutType(mDevices[position].getType_id());
	}

	public void setBackgroundTxtName(int type, boolean isOn) {

	}

	public boolean IsCheckOnOff() {
		return ischeckTypeOnOff;
	}

	public void setIsCheckDim(boolean ischeck) {
		ischeckTypeDimmer = ischeck;
	}

	public boolean IsCheckDim() {
		return ischeckTypeDimmer;
	}

	public void setIsCheckRGB(boolean ischeck) {
		ischeckTypeRGB = ischeck;
	}

	public boolean IsCheckRGB() {
		return ischeckTypeRGB;
	}

	public void setIsCheckBlind(boolean ischeck) {
		ischeckTypeBlind = ischeck;
	}

	public boolean IsCheckBlind() {
		return ischeckTypeBlind;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		ViewHolder_ONOff viewHolder_Off = null;
		ViewHolder_Hor_Blind viewHolder_Hor_Blind = null;
		ViewHolder_Ver_Blind viewHolder_Ver_Blind = null;
		ViewHolder_Dimmer viewHolder_Dimmer = null;
		ViewHolder_RGB viewHolder_RGB = null;

		int layout_type = getItemViewType(position);
		if (convertView == null) {
			switch (layout_type) {

			case Device.TYPE_LAYOUT_ONOFF:
				convertView = View.inflate(mContext,
						R.layout.item_listview_device_onoff, null);
				viewHolder_Off = new ViewHolder_ONOff();
				viewHolder_Off.imgDeviceImage = (ImageView) convertView
						.findViewById(R.id.item_listview_device_onoff_img_onoff);
				viewHolder_Off.txvDeviceName = (TextView) convertView
						.findViewById(R.id.item_listview_device_onoff_txt_name);
				viewHolder_Off.imgIsAddedIntoScene = (ImageView) convertView
						.findViewById(R.id.item_listview_device_onoff_img_schedule);

				convertView.setTag(viewHolder_Off);
				break;
			case Device.TYPE_LAYOUT_DIMMER:
				convertView = View.inflate(mContext,
						R.layout.item_list_device_dimmer, null);
				viewHolder_Dimmer = new ViewHolder_Dimmer();
				viewHolder_Dimmer.txvDeviceName = (TextView) convertView
						.findViewById(R.id.item_listview_device_dimmer_txt_name);
				viewHolder_Dimmer.imgIsAddedIntoScene = (ImageView) convertView
						.findViewById(R.id.item_listview_device_dimmer_img_schedule);
				viewHolder_Dimmer.imgDeviceImageDim = (ImageView) convertView
						.findViewById(R.id.item_listview_device_dimmer_img_onoff);
				viewHolder_Dimmer.skbDimmerValue = (SeekBar) convertView
						.findViewById(R.id.item_listview_device_valuechange_seekbar_change);

				convertView.setTag(viewHolder_Dimmer);
				break;
			case Device.TYPE_LAYOUT_HOR_BLIND:
				convertView = View.inflate(mContext,
						R.layout.item_list_device_blind, null);
				viewHolder_Hor_Blind = new ViewHolder_Hor_Blind();
				viewHolder_Hor_Blind.imgBlind = (ImageView) convertView
						.findViewById(R.id.item_list_device_blind_img_onoff);
				viewHolder_Hor_Blind.txvDeviceName = (TextView) convertView
						.findViewById(R.id.item_list_device_blind_txt_name);
				viewHolder_Hor_Blind.imgIsAddedIntoScene = (ImageView) convertView
						.findViewById(R.id.item_list_device_blind_img_schedule);
				viewHolder_Hor_Blind.imgBlindOpen = (ImageView) convertView
						.findViewById(R.id.item_list_device_blind_left);
				viewHolder_Hor_Blind.imgBlindClose = (ImageView) convertView
						.findViewById(R.id.item_list_device_blind_right);

				convertView.setTag(viewHolder_Hor_Blind);
				break;
			case Device.TYPE_LAYOUT_VER_BLIND:
				convertView = View.inflate(mContext,
						R.layout.item_list_device_blind, null);
				viewHolder_Ver_Blind = new ViewHolder_Ver_Blind();
				viewHolder_Ver_Blind.imgBlind = (ImageView) convertView
						.findViewById(R.id.item_list_device_blind_img_onoff);
				viewHolder_Ver_Blind.txvDeviceName = (TextView) convertView
						.findViewById(R.id.item_list_device_blind_txt_name);
				viewHolder_Ver_Blind.imgIsAddedIntoScene = (ImageView) convertView
						.findViewById(R.id.item_list_device_blind_img_schedule);
				viewHolder_Ver_Blind.imgBlindOpen = (ImageView) convertView
						.findViewById(R.id.item_list_device_blind_left);
				viewHolder_Ver_Blind.imgBlindClose = (ImageView) convertView
						.findViewById(R.id.item_list_device_blind_right);

				convertView.setTag(viewHolder_Ver_Blind);
				break;

			case Device.TYPE_LAYOUT_RGB:
				convertView = View.inflate(mContext,
						R.layout.item_list_device_rgb, null);
				viewHolder_RGB = new ViewHolder_RGB();
				viewHolder_RGB.txvDeviceName = (TextView) convertView
						.findViewById(R.id.item_list_device_rgb_txt_name);
				viewHolder_RGB.imgIsAddedIntoScene = (ImageView) convertView
						.findViewById(R.id.item_list_device_rgb_img_schedule);
				viewHolder_RGB.colorPickerPanelView = (ColorPickerPanelView) convertView
						.findViewById(R.id.item_list_device_rgb_color_panel);

				convertView.setTag(viewHolder_RGB);
				break;

			default:
				break;
			}
		} else {
			switch (layout_type) {
			case Device.TYPE_LAYOUT_ONOFF:
				viewHolder_Off = (ViewHolder_ONOff) convertView.getTag();
				break;
			case Device.TYPE_LAYOUT_DIMMER:
				viewHolder_Dimmer = (ViewHolder_Dimmer) convertView.getTag();
				break;
			case Device.TYPE_LAYOUT_VER_BLIND:
				viewHolder_Ver_Blind = (ViewHolder_Ver_Blind) convertView
						.getTag();
				break;
			case Device.TYPE_LAYOUT_HOR_BLIND:
				viewHolder_Hor_Blind = (ViewHolder_Hor_Blind) convertView
						.getTag();
				break;
			case Device.TYPE_LAYOUT_RGB:
				viewHolder_RGB = (ViewHolder_RGB) convertView.getTag();
				break;

			default:
				break;
			}

		}

		switch (layout_type) {
		case Device.TYPE_LAYOUT_ONOFF:

			// set name
			viewHolder_Off.txvDeviceName.setText(mDevices[position].getName());
			// set checkbox
			if (mDevices[position].isSelected()) {
				viewHolder_Off.imgIsAddedIntoScene
						.setImageResource(R.drawable.img_tickon);
				viewHolder_Off.txvDeviceName
						.setBackgroundResource(R.drawable.img_bg_dev_normal_name_on);
			} else {
				viewHolder_Off.imgIsAddedIntoScene
						.setImageResource(R.drawable.img_tickoff);
				viewHolder_Off.txvDeviceName
						.setBackgroundResource(R.drawable.img_bg_dev_normal_name_off);
			}

			// checkbox onclick
			viewHolder_Off.imgIsAddedIntoScene
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							if (Config.isVibrate)
								new VibrateThread(mContext).start();
							if (mDevices[position].isSelected()) {
								mDevices[position].setSelected(false);
								((ImageView) v)
										.setImageResource(R.drawable.img_tickoff);
								notifyDataSetChanged();

							} else {
								mDevices[position].setSelected(true);
								((ImageView) v)
										.setImageResource(R.drawable.img_tickon);
								notifyDataSetChanged();
							}
						}
					});

			// set device image
			if (mDevices[position].getDeviceState().getValue() == Device.DEVICE_STATE_DEVICE_OFF) {
				switch (mDevices[position].getType_id()) {
				case Device.DEVICE_TYPE_LIGHT:
					viewHolder_Off.imgDeviceImage
							.setImageResource(R.drawable.img_light_off_selector);
					break;
				case Device.DEVICE_TYPE_HEATER:
					viewHolder_Off.imgDeviceImage
							.setImageResource(R.drawable.img_heater_off_selector);
					break;
				case Device.DEVICE_TYPE_AIRCON:
					viewHolder_Off.imgDeviceImage
							.setImageResource(R.drawable.img_aircon_off_selector);
					break;
				case Device.DEVICE_TYPE_MULTIROOM_AUDIO:
					viewHolder_Off.imgDeviceImage
							.setImageResource(R.drawable.img_multiroom_audio_off_selector);
					break;
				case Device.DEVICE_TYPE_GATE:
					viewHolder_Off.imgDeviceImage
					.setImageResource(R.drawable.img_gate_1_selector);
					break;
				default:
					break;
				}
			} else if (mDevices[position].getDeviceState().getValue() == Device.DEVICE_STATE_DEVICE_ON) {
				switch (mDevices[position].getType_id()) {
				case Device.DEVICE_TYPE_LIGHT:
					viewHolder_Off.imgDeviceImage
							.setImageResource(R.drawable.img_light_on_selector);
					break;
				case Device.DEVICE_TYPE_HEATER:
					viewHolder_Off.imgDeviceImage
							.setImageResource(R.drawable.img_heater_on_selector);
					break;
				case Device.DEVICE_TYPE_AIRCON:
					viewHolder_Off.imgDeviceImage
							.setImageResource(R.drawable.img_aircon_on_selector);
					break;
				case Device.DEVICE_TYPE_MULTIROOM_AUDIO:
					viewHolder_Off.imgDeviceImage
							.setImageResource(R.drawable.img_multiroom_audio_on_selector);
					break;
				case Device.DEVICE_TYPE_GATE:
					viewHolder_Off.imgDeviceImage
					.setImageResource(R.drawable.img_gate_8_selector);
					break;	
				default:
					break;
				}
			}

			// device image onclick
			viewHolder_Off.imgDeviceImage
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							if (Config.isVibrate)
								new VibrateThread(mContext).start();
							if (mDevices[position].getDeviceState().getValue() == Device.DEVICE_STATE_DEVICE_OFF) {
								mDevices[position].getDeviceState().setValue(
										Device.DEVICE_STATE_DEVICE_ON);
								switch (mDevices[position].getType_id()) {
								case Device.DEVICE_TYPE_LIGHT:
									((ImageView) v)
											.setImageResource(R.drawable.img_light_on_selector);
									break;
								case Device.DEVICE_TYPE_HEATER:
									((ImageView) v)
											.setImageResource(R.drawable.img_heater_on_selector);
									break;
								case Device.DEVICE_TYPE_AIRCON:
									((ImageView) v)
											.setImageResource(R.drawable.img_aircon_on_selector);
									break;
								case Device.DEVICE_TYPE_MULTIROOM_AUDIO:
									((ImageView) v)
											.setImageResource(R.drawable.img_multiroom_audio_on_selector);
									break;	
								case Device.DEVICE_TYPE_GATE:
									((ImageView) v)
											.setImageResource(R.drawable.img_gate_8_selector);
									break;		
								default:
									break;
								}
							} else if (mDevices[position].getDeviceState()
									.getValue() == Device.DEVICE_STATE_DEVICE_ON) {
								mDevices[position].setTurnOn(false);
								mDevices[position].getDeviceState().setValue(
										Device.DEVICE_STATE_DEVICE_OFF);
								switch (mDevices[position].getType_id()) {
								case Device.DEVICE_TYPE_LIGHT:
									((ImageView) v)
											.setImageResource(R.drawable.img_light_off_selector);
									break;
								case Device.DEVICE_TYPE_HEATER:
									((ImageView) v)
											.setImageResource(R.drawable.img_heater_off_selector);
									break;
								case Device.DEVICE_TYPE_AIRCON:
									((ImageView) v)
											.setImageResource(R.drawable.img_aircon_off_selector);
									break;
								case Device.DEVICE_TYPE_MULTIROOM_AUDIO:
									((ImageView) v)
											.setImageResource(R.drawable.img_multiroom_audio_off_selector);
									break;	
								case Device.DEVICE_TYPE_GATE:
									((ImageView) v)
											.setImageResource(R.drawable.img_gate_1_selector);
									break;		
								default:
									break;
								}
							}
						}
					});
			break;

		case Device.TYPE_LAYOUT_DIMMER:
			// set name
			viewHolder_Dimmer.txvDeviceName.setText(mDevices[position]
					.getName());

			// set seekbar
			if (mDevices[position].getType_id() == Device.DEVICE_TYPE_DIMMER)
				viewHolder_Dimmer.skbDimmerValue
						.setMax(Device.DEVICE_STATE_DIMMER_MAX_LEVEL - 1);
			else if (mDevices[position].getType_id() == Device.DEVICE_TYPE_FAN)
				viewHolder_Dimmer.skbDimmerValue
						.setMax((Device.DEVICE_STATE_FAN_MAX_LEVEL - 1));
			viewHolder_Dimmer.skbDimmerValue.setProgress(mDevices[position]
					.getDeviceState().getValue() - 1);

			// seekbar onChangeListener
			OnSeekBarChangeListener seekBarChangeListener = new OnSeekBarChangeListener() {

				@Override
				public void onStopTrackingTouch(SeekBar seekBar) {
					if (Config.isVibrate)
						new VibrateThread(mContext).start();
					mDevices[position].getDeviceState().setValue(
							seekBar.getProgress() + 1);
					LogUltis.i(TAG,
							"Device Name=" + mDevices[position].getName());
					LogUltis.i(TAG, "Device Value="
							+ mDevices[position].getDeviceState().getValue());
					notifyDataSetChanged();
				}

				@Override
				public void onStartTrackingTouch(SeekBar seekBar) {

				}

				@Override
				public void onProgressChanged(SeekBar seekBar, int progress,
						boolean fromUser) {

				}
			};
			viewHolder_Dimmer.skbDimmerValue
					.setOnSeekBarChangeListener(seekBarChangeListener);

			// set checkbox
			if (mDevices[position].isSelected()) {
				viewHolder_Dimmer.imgIsAddedIntoScene
						.setImageResource(R.drawable.img_tickon);
			} else {
				viewHolder_Dimmer.imgIsAddedIntoScene
						.setImageResource(R.drawable.img_tickoff);
			}

			// checkbox onclick
			viewHolder_Dimmer.imgIsAddedIntoScene
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							if (Config.isVibrate)
								new VibrateThread(mContext).start();
							if (mDevices[position].isSelected()) {
								mDevices[position].setSelected(false);
								((ImageView) v)
										.setImageResource(R.drawable.img_tickoff);

							} else {
								mDevices[position].setSelected(true);
								((ImageView) v)
										.setImageResource(R.drawable.img_tickon);

							}

						}
					});

			if (mDevices[position].getDeviceState().getValue() == Device.DEVICE_STATE_DIMMER_OFF)
				viewHolder_Dimmer.imgDeviceImageDim
						.setImageResource(R.drawable.img_light_off_selector);
			else {
				viewHolder_Dimmer.imgDeviceImageDim
						.setImageResource(R.drawable.img_light_on_selector);
			}
			viewHolder_Dimmer.imgDeviceImageDim
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							if (Config.isVibrate)
								new VibrateThread(mContext).start();
							if (mDevices[position].getDeviceState().getValue() == Device.DEVICE_STATE_DIMMER_OFF) {
								mDevices[position].getDeviceState().setValue(
										Device.DEVICE_STATE_DIMMER_ON);
								notifyDataSetChanged();
							} else {
								mDevices[position].getDeviceState().setValue(
										Device.DEVICE_STATE_DIMMER_OFF);
								notifyDataSetChanged();
							}
						}
					});

			break;

		case Device.TYPE_LAYOUT_VER_BLIND:
			// final ViewHolder_Blind mViewHolder_Blind = viewHolder_Blind;
			// set name
			viewHolder_Ver_Blind.txvDeviceName.setText(mDevices[position]
					.getName());
			// set image
			final int blind_position = mDevices[position].getDeviceState()
					.getValue();
			switch (blind_position) {
			case Device.DEVICE_STATE_BLIND_MAX_LEVEL:
				viewHolder_Ver_Blind.imgBlind
						.setImageResource(R.drawable.ic_ver_blind_8);
				break;
			case Device.DEVICE_STATE_BLIND_MIN_LEVEL:
				viewHolder_Ver_Blind.imgBlind
						.setImageResource(R.drawable.ic_ver_blind_1);
				break;
			case Device.DEVICE_STATE_BLIND_LEVEL_2:
				viewHolder_Ver_Blind.imgBlind
						.setImageResource(R.drawable.ic_ver_blind_2);
				break;
			case Device.DEVICE_STATE_BLIND_LEVEL_3:
				viewHolder_Ver_Blind.imgBlind
						.setImageResource(R.drawable.ic_ver_blind_3);
				break;
			case Device.DEVICE_STATE_BLIND_LEVEL_4:
				viewHolder_Ver_Blind.imgBlind
						.setImageResource(R.drawable.ic_ver_blind_4);
				break;
			case Device.DEVICE_STATE_BLIND_LEVEL_5:
				viewHolder_Ver_Blind.imgBlind
						.setImageResource(R.drawable.ic_ver_blind_5);
				break;
			case Device.DEVICE_STATE_BLIND_LEVEL_6:
				viewHolder_Ver_Blind.imgBlind
						.setImageResource(R.drawable.ic_ver_blind_6);
				break;
			case Device.DEVICE_STATE_BLIND_LEVEL_7:
				viewHolder_Ver_Blind.imgBlind
						.setImageResource(R.drawable.ic_ver_blind_7);
				break;

			default:
				mDevices[position].getDeviceState().setValue(
						Device.DEVICE_STATE_BLIND_MIN_LEVEL);
				viewHolder_Ver_Blind.imgBlind
						.setImageResource(R.drawable.ic_ver_blind_1);
				break;

			}

			// move left onclick
			viewHolder_Ver_Blind.imgBlind
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							if (Config.isVibrate)
								new VibrateThread(mContext).start();
							int current_position = mDevices[position]
									.getDeviceState().getValue();
							if (current_position >= Device.DEVICE_STATE_BLIND_MAX_LEVEL / 2) {
								mDevices[position].getDeviceState().setValue(
										Device.DEVICE_STATE_BLIND_MIN_LEVEL);
								((ImageView) arg0)
										.setImageResource(R.drawable.ic_ver_blind_1);
							} else if (current_position < Device.DEVICE_STATE_BLIND_MAX_LEVEL / 2) {
								mDevices[position].getDeviceState().setValue(
										Device.DEVICE_STATE_BLIND_MAX_LEVEL);
								((ImageView) arg0)
										.setImageResource(R.drawable.ic_ver_blind_8);
							}
						}
					});
			viewHolder_Ver_Blind.imgBlindOpen
					.setImageResource(R.drawable.img_ver_blind_open);
			viewHolder_Ver_Blind.imgBlindOpen
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							if (Config.isVibrate)
								new VibrateThread(mContext).start();
							int current_position = mDevices[position]
									.getDeviceState().getValue();
							if (current_position > Device.DEVICE_STATE_BLIND_MIN_LEVEL) {
								current_position--;
								mDevices[position].getDeviceState().setValue(
										current_position);
								notifyDataSetChanged();

							} else {
								mDevices[position].getDeviceState().setValue(
										current_position);
								notifyDataSetChanged();
							}

						}
					});
			viewHolder_Ver_Blind.imgBlindClose
					.setImageResource(R.drawable.img_ver_blind_close);
			viewHolder_Ver_Blind.imgBlindClose
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							if (Config.isVibrate)
								new VibrateThread(mContext).start();
							int current_position = mDevices[position]
									.getDeviceState().getValue();
							if (current_position < Device.DEVICE_STATE_BLIND_MAX_LEVEL) {
								current_position++;
								mDevices[position].getDeviceState().setValue(
										current_position);
								notifyDataSetChanged();
							} else {
								mDevices[position].getDeviceState().setValue(
										current_position);
								notifyDataSetChanged();
							}
						}
					});

			// set checkbox
			if (mDevices[position].isSelected()) {
				viewHolder_Ver_Blind.imgIsAddedIntoScene
						.setImageResource(R.drawable.img_tickon);
			} else {
				viewHolder_Ver_Blind.imgIsAddedIntoScene
						.setImageResource(R.drawable.img_tickoff);
			}
			// checkbox onclick
			viewHolder_Ver_Blind.imgIsAddedIntoScene
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							if (Config.isVibrate)
								new VibrateThread(mContext).start();
							if (mDevices[position].isSelected()) {
								mDevices[position].setSelected(false);
								((ImageView) v)
										.setImageResource(R.drawable.img_tickoff);

							} else {
								mDevices[position].setSelected(true);
								((ImageView) v)
										.setImageResource(R.drawable.img_tickon);
							}
						}
					});
			break;
		case Device.TYPE_LAYOUT_HOR_BLIND:
			// final ViewHolder_Blind mViewHolder_Blind = viewHolder_Blind;
			// set name
			viewHolder_Hor_Blind.txvDeviceName.setText(mDevices[position]
					.getName());
			// set image
			final int blind_position_hor = mDevices[position].getDeviceState()
					.getValue();
			switch (blind_position_hor) {
			case Device.DEVICE_STATE_BLIND_MAX_LEVEL:
				viewHolder_Hor_Blind.imgBlind
						.setImageResource(R.drawable.ic_hor_blind_8);
				break;
			case Device.DEVICE_STATE_BLIND_MIN_LEVEL:
				viewHolder_Hor_Blind.imgBlind
						.setImageResource(R.drawable.ic_hor_blind_1);
				break;
			case Device.DEVICE_STATE_BLIND_LEVEL_2:
				viewHolder_Hor_Blind.imgBlind
						.setImageResource(R.drawable.ic_hor_blind_2);
				break;
			case Device.DEVICE_STATE_BLIND_LEVEL_3:
				viewHolder_Hor_Blind.imgBlind
						.setImageResource(R.drawable.ic_hor_blind_3);
				break;
			case Device.DEVICE_STATE_BLIND_LEVEL_4:
				viewHolder_Hor_Blind.imgBlind
						.setImageResource(R.drawable.ic_hor_blind_4);
				break;
			case Device.DEVICE_STATE_BLIND_LEVEL_5:
				viewHolder_Hor_Blind.imgBlind
						.setImageResource(R.drawable.ic_hor_blind_5);
				break;
			case Device.DEVICE_STATE_BLIND_LEVEL_6:
				viewHolder_Hor_Blind.imgBlind
						.setImageResource(R.drawable.ic_hor_blind_6);
				break;
			case Device.DEVICE_STATE_BLIND_LEVEL_7:
				viewHolder_Hor_Blind.imgBlind
						.setImageResource(R.drawable.ic_hor_blind_7);
				break;

			default:
				mDevices[position].getDeviceState().setValue(
						Device.DEVICE_STATE_BLIND_MIN_LEVEL);
				viewHolder_Hor_Blind.imgBlind
						.setImageResource(R.drawable.ic_hor_blind_1);
				break;

			}

			// move left onclick
			viewHolder_Hor_Blind.imgBlind
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							if (Config.isVibrate)
								new VibrateThread(mContext).start();
							int current_position = mDevices[position]
									.getDeviceState().getValue();
							if (current_position == Device.DEVICE_STATE_BLIND_MAX_LEVEL) {
								mDevices[position].getDeviceState().setValue(
										Device.DEVICE_STATE_BLIND_MIN_LEVEL);
								((ImageView) arg0)
										.setImageResource(R.drawable.ic_hor_blind_1);
							} else if (current_position == Device.DEVICE_STATE_BLIND_MIN_LEVEL) {
								mDevices[position].getDeviceState().setValue(
										Device.DEVICE_STATE_BLIND_MAX_LEVEL);
								((ImageView) arg0)
										.setImageResource(R.drawable.ic_hor_blind_8);
							}
						}
					});
			viewHolder_Hor_Blind.imgBlindOpen
					.setImageResource(R.drawable.img_hor_blind_open);
			viewHolder_Hor_Blind.imgBlindOpen
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							if (Config.isVibrate)
								new VibrateThread(mContext).start();
							int current_position = mDevices[position]
									.getDeviceState().getValue();
							if (current_position > Device.DEVICE_STATE_BLIND_MIN_LEVEL) {
								current_position--;
								mDevices[position].getDeviceState().setValue(
										current_position);
								notifyDataSetChanged();

							} else {
								mDevices[position].getDeviceState().setValue(
										current_position);
								notifyDataSetChanged();
							}

						}
					});
			viewHolder_Hor_Blind.imgBlindClose
					.setImageResource(R.drawable.img_hor_blind_close);
			viewHolder_Hor_Blind.imgBlindClose
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							if (Config.isVibrate)
								new VibrateThread(mContext).start();
							int current_position = mDevices[position]
									.getDeviceState().getValue();
							if (current_position < Device.DEVICE_STATE_BLIND_MAX_LEVEL) {
								current_position++;
								mDevices[position].getDeviceState().setValue(
										current_position);
								notifyDataSetChanged();
							} else {
								mDevices[position].getDeviceState().setValue(
										current_position);
								notifyDataSetChanged();
							}
						}
					});

			// set checkbox
			if (mDevices[position].isSelected()) {
				viewHolder_Hor_Blind.imgIsAddedIntoScene
						.setImageResource(R.drawable.img_tickon);
			} else {
				viewHolder_Hor_Blind.imgIsAddedIntoScene
						.setImageResource(R.drawable.img_tickoff);
			}
			// checkbox onclick
			viewHolder_Hor_Blind.imgIsAddedIntoScene
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							if (Config.isVibrate)
								new VibrateThread(mContext).start();
							if (mDevices[position].isSelected()) {
								mDevices[position].setSelected(false);
								((ImageView) v)
										.setImageResource(R.drawable.img_tickoff);

							} else {
								mDevices[position].setSelected(true);
								((ImageView) v)
										.setImageResource(R.drawable.img_tickon);
							}
						}
					});
			break;
		case Device.TYPE_LAYOUT_RGB:
			// set name
			viewHolder_RGB.txvDeviceName.setText(mDevices[position].getName());
			// set color
			int color = mDevices[position].getDeviceState().getRGB();
			if (Math.abs(color) < Math.abs(Device.DEVICE_STATE_RGB_OFF))
				color = Device.DEVICE_STATE_RGB_OFF;
			color += 0xFF000000;
			viewHolder_RGB.colorPickerPanelView.setColor(color);
			// color onclick
			final ViewHolder_RGB mViewHolder_RGB = viewHolder_RGB;
			final OnTouchReleasedListener onTouchReleasedListener = new OnTouchReleasedListener() {
				@Override
				public void onTouchReleased(int color) {

					mViewHolder_RGB.colorPickerPanelView.setColor(color);
					// convert color
					color = Math.abs(color - 0xFF000000);
					int Blue = color % 256;
					if (Blue < Math.abs(0xFF))
						Blue++;
					color = color / 256;
					int Green = color % 256;
					if (Green < Math.abs(0xFF))
						Green++;
					color = color / 256;
					int Red = color % 256;
					if (Red < Math.abs(0xFF))
						Red++;
					color = Blue + 256 * Green + 256 * 256 * Red;
					Log.e("color", "" + color);
					mDevices[position].getDeviceState().setRGB(color);
					notifyDataSetChanged();
				}
			};
			final OnColorChangedListener onColorChangedListener = new OnColorChangedListener() {

				@Override
				public void onColorChanged(int color) {

				}

				@Override
				public void onCancelDialog() {
					// TODO Auto-generated method stub

				}
			};
			viewHolder_RGB.colorPickerPanelView
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							if (Config.isVibrate)
								new VibrateThread(mContext).start();
							int color = mViewHolder_RGB.colorPickerPanelView
									.getColor();
							showColorPickerDialog(onColorChangedListener,
									onTouchReleasedListener, color);
						}
					});
			// set checkbox
			if (mDevices[position].isSelected()) {
				viewHolder_RGB.imgIsAddedIntoScene
						.setImageResource(R.drawable.img_tickon);
			} else {
				viewHolder_RGB.imgIsAddedIntoScene
						.setImageResource(R.drawable.img_tickoff);
			}
			// checkbox onclick
			viewHolder_RGB.imgIsAddedIntoScene
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							if (Config.isVibrate)
								new VibrateThread(mContext).start();
							if (mDevices[position].isSelected()) {
								mDevices[position].setSelected(false);
								((ImageView) v)
										.setImageResource(R.drawable.img_tickoff);
							} else {
								mDevices[position].setSelected(true);
								((ImageView) v)
										.setImageResource(R.drawable.img_tickon);
							}

						}
					});
			break;
		default:
			break;
		}
		return convertView;
	}

	public Device[] getDevices() {
		return mDevices;
	}

	private abstract static class BaseViewHolder {
		public TextView txvDeviceName;
		public ImageView imgIsAddedIntoScene;

		public abstract void setOnOffTxtName(boolean isOn);

	}

	private static class ViewHolder_ONOff extends BaseViewHolder {
		public ImageView imgDeviceImage;

		/*
		 * (non-Javadoc)
		 * 
		 * @see vn.lumi.smarthome.ui.DeviceListAdapter.BaseViewHolder#setOnOffTxtName (boolean)
		 */
		@Override
		public void setOnOffTxtName(boolean isOn) {
			// TODO Auto-generated method stub
			if (isOn)
				txvDeviceName
						.setBackgroundResource(R.drawable.img_bg_dev_normal_name_on);
			else
				txvDeviceName
						.setBackgroundResource(R.drawable.img_bg_dev_normal_name_off);
		}
	}

	private static class ViewHolder_Dimmer extends BaseViewHolder {
		public SeekBar skbDimmerValue;
		public ImageView imgDeviceImageDim;

		/*
		 * (non-Javadoc)
		 * 
		 * @see vn.lumi.smarthome.ui.DeviceListAdapter.BaseViewHolder#setOnOffTxtName (boolean)
		 */
		@Override
		public void setOnOffTxtName(boolean isOn) {
			// TODO Auto-generated method stub

		}
	}

	private static class ViewHolder_Hor_Blind extends BaseViewHolder {
		public ImageView imgBlind;
		// public ImageView imgClose;
		public ImageView imgBlindOpen;
		public ImageView imgBlindClose;

		/*
		 * (non-Javadoc)
		 * 
		 * @see vn.lumi.smarthome.ui.DeviceListAdapter.BaseViewHolder#setOnOffTxtName (boolean)
		 */
		@Override
		public void setOnOffTxtName(boolean isOn) {
			// TODO Auto-generated method stub

		}
	}

	private static class ViewHolder_Ver_Blind extends BaseViewHolder {
		public ImageView imgBlind;
		// public ImageView imgClose;
		public ImageView imgBlindOpen;
		public ImageView imgBlindClose;

		/*
		 * (non-Javadoc)
		 * 
		 * @see vn.lumi.smarthome.ui.DeviceListAdapter.BaseViewHolder#setOnOffTxtName (boolean)
		 */
		@Override
		public void setOnOffTxtName(boolean isOn) {
			// TODO Auto-generated method stub

		}
	}

	private static class ViewHolder_RGB extends BaseViewHolder {
		public ColorPickerPanelView colorPickerPanelView;

		/*
		 * (non-Javadoc)
		 * 
		 * @see vn.lumi.smarthome.ui.DeviceListAdapter.BaseViewHolder#setOnOffTxtName (boolean)
		 */
		@Override
		public void setOnOffTxtName(boolean isOn) {
			// TODO Auto-generated method stub

		}
	}

	protected void showColorPickerDialog(
			OnColorChangedListener onColorChangedListener,
			OnTouchReleasedListener onTouchReleasedListener, int value) {
		mDialog = new ColorPickerDialog(mContext, value);
		mDialog.setOnColorChangedListener(onColorChangedListener);
		mDialog.setOnTouchReleasedListener(onTouchReleasedListener);
		mDialog.setCancelable(false);
		mDialog.show();
	}
}
